﻿using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace DiminishedReturn
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow
	{
		private readonly ObservableCollection<double> _intermediates;
		private readonly ObservableCollection<double> _values;
		public MainWindow()
		{
			InitializeComponent();
			btnCalculate.Click += BtnCalculateClick;
			_intermediates = new ObservableCollection<double>();
			_values = new ObservableCollection<double>();
			lstIntermediateValues.ItemsSource = _intermediates;
			lstValueIncrease.ItemsSource = _values;
		}

		void BtnCalculateClick(object sender, RoutedEventArgs e)
		{
			double value;
			if (!double.TryParse(txtValue.Text, out value)) return;
			double decrease;
			if (!double.TryParse(txtDecrease.Text, out decrease)) return;
			double stacksize;
			if (!double.TryParse(txtStacksize.Text, out stacksize)) return;

			_intermediates.Clear();
			_values.Clear();
			double result = 0d;
			for(int i = 0; i < stacksize; i++)
			{
				double intermediate = Math.Pow(decrease, i)*value;
				_intermediates.Add(intermediate);
				double val = result + intermediate;
				_values.Add(val);
				result += intermediate;
			}
			txtResult.Text = result.ToString();
		}
	}
}